package org.dromara.system.controller.device;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.*;
import org.dromara.system.service.IDeviceService;
import org.dromara.system.service.IGatewayReportService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备信息
 *
 * @author Lion Li
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/device")
public class DeviceController extends BaseController {

    private final IDeviceService deviceService;

    private final IGatewayReportService gatewayReportService;

    /**
     * 查询设备信息列表
     */
    @SaCheckPermission("system:device:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceVo> list(DeviceBo bo, PageQuery pageQuery) {
        return deviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取设备信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:device:list")
    @GetMapping("/{id}")
    public R<DeviceVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long id) {
        return R.ok(deviceService.getDeviceInfoById(id));
    }

    /**
     * 新增设备信息
     */
    @SaCheckPermission("system:device:list")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceBo bo) {
        return toAjax(deviceService.insertByBo(bo));
    }

    /**
     * 修改设备信息
     */
    @SaCheckPermission("system:device:list")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceBo bo) {
        return toAjax(deviceService.updateByBo(bo));
    }

    /**
     * 删除设备信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:device:list")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(deviceService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaCheckPermission("system:device:list")
    @Log(title = "设备和变量绑定", businessType = BusinessType.OTHER)
    @PostMapping("/setVariableAssociation/{deviceId}")
    public R<Void> setVariableAssociation(@PathVariable Long deviceId, @RequestBody IdsBo idsBo) {
        List<Long> idsList = Arrays.stream(idsBo.getIds()).collect(Collectors.toList());
        return toAjax(deviceService.setVariableAssociation(deviceId, idsList));
//        return R.ok();
    }

    @SaCheckPermission("system:device:list")
    @Log(title = "设备和变量绑定", businessType = BusinessType.OTHER)
    @PostMapping("/unbindDeviceVariable/{deviceId}")
    public R<Void> unbindDeviceVariable(@PathVariable Long deviceId, @RequestBody IdBo idBo) {
        return toAjax(deviceService.unbindDeviceVariable(deviceId, idBo.getId()));
    }

    @SaCheckPermission("system:device:list")
    @Log(title = "设备新增一个变量绑定", businessType = BusinessType.OTHER)
    @PostMapping("/bindDeviceVariable/{deviceId}")
    public R<Void> bindDeviceVariable(@PathVariable Long deviceId, @RequestBody IdBo idBo) {
        return toAjax(deviceService.bindDeviceVariable(deviceId, idBo.getId()));
    }

    @SaCheckPermission("system:device:list")
    @Log(title = "获取某一个变量的5000", businessType = BusinessType.OTHER)
    @GetMapping("/getDeviceVariableValueList/{id}")
    public R<List<SensorVo>> getDeviceVariableValueList(@PathVariable Long id) {
        return R.ok(deviceService.getDeviceVariableValueList(id));
    }

    @SaCheckPermission("system:device:list")
    @GetMapping("/getHistoryList")
    @Log(title = "历史记录", businessType = BusinessType.OTHER)
    public TableDataInfo<HistoryVo> getDeviceVariableValueList(SensorBo bo, PageQuery pageQuery) {
        return deviceService.getHistoryList(bo, pageQuery);
    }

    @SaCheckPermission("system:device:list")
    @Log(title = "查询设备以及旗下变量信息-历史数据使用", businessType = BusinessType.OTHER)
    @GetMapping("/getDeviceVariableList")
    public TableDataInfo<DeviceNameVo> getDeviceVariableList(groupIdsBo bo, PageQuery pageQuery) {
        return deviceService.getDeviceVariableList(bo.getIds(), pageQuery);
    }



    @SaIgnore
    @GetMapping("/gatewayReport")
    public TableDataInfo<GatewayReportVo> gatewayReport(GatewayReportBo bo, PageQuery pageQuery) {
        return gatewayReportService.selectPageList(bo, pageQuery);
    }


}
